package com.ruoyi.web.controller.guarantee;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.guarantee.domain.BAgreementPosition;
import com.ruoyi.guarantee.service.IBAgreementPositionService;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.guarantee.domain.BAgreement;
import com.ruoyi.guarantee.service.IBAgreementService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
 * 履约保函Controller
 * 
 * @author ruoyi
 * @date 2024-11-05
 */
@RestController
@RequestMapping("/guarantee/agreement")
public class BAgreementController extends BaseController
{
    @Autowired
    private IBAgreementService bAgreementService;
    @Autowired
    private IBAgreementPositionService ibAgreementPositionService;

    /**
     * 查询履约保函列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BAgreement bAgreement)
    {
        bAgreement.setUserId(getUserId());
        startPage();
        List<BAgreement> list = bAgreementService.selectBAgreementList(bAgreement);
        return getDataTable(list);
    }

    /**
     * 导出履约保函列表
     */
    @Log(title = "履约保函", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BAgreement bAgreement)
    {
        List<BAgreement> list = bAgreementService.selectBAgreementList(bAgreement);
        ExcelUtil<BAgreement> util = new ExcelUtil<BAgreement>(BAgreement.class);
        util.exportExcel(response, list, "履约保函数据");
    }

    /**
     * 获取履约保函详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        BAgreement bAgreement = bAgreementService.selectBAgreementById(id);
        if(bAgreement.getXmNameLot()!=null){
            bAgreement.setXmNameLotList(bAgreement.getXmNameLot().split(","));
        }

        return success(bAgreement);
    }

    /**
     * 新增履约保函
     */
    @Log(title = "履约保函", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BAgreement bAgreement)
    {
        String xmNameLot = arrayToString(bAgreement.getXmNameLotList(), ", ");
        bAgreement.setXmNameLot(xmNameLot);
        bAgreement.setStatus(0L);
        bAgreement.setUserId(getUserId());
        bAgreement.setCreateBy(getUsername());
        return toAjax(bAgreementService.insertBAgreement(bAgreement));
    }


    public static String arrayToString(String[] array, String delimiter) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            if (i > 0) {
                builder.append(delimiter);
            }
            builder.append(array[i]);
        }
        return builder.toString();
    }

    /**
     * 修改履约保函
     */
    @Log(title = "履约保函", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BAgreement bAgreement)
    {
        return toAjax(bAgreementService.updateBAgreement(bAgreement));
    }

    /**
     * 删除履约保函
     */
    @Log(title = "履约保函", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bAgreementService.deleteBAgreementByIds(ids));
    }

    /**
     * 履约保函提交
     */
    @Log(title = "履约保函", businessType = BusinessType.DELETE)
    @PostMapping("/tijiao")
    public AjaxResult tijiao( @RequestBody BAgreement agreement)
    {
        BAgreement bAgreement = bAgreementService.selectBAgreementById(agreement.getId());
        if (bAgreement == null) {
            return AjaxResult.error("该保函不存在");
        }
        Long status = bAgreement.getStatus();
        if(status == 0||status == -1){
            bAgreement.setStatus(2L);
            return toAjax(bAgreementService.updateBAgreement(bAgreement));
        }
        return AjaxResult.error("该保函已提交");
    }

    /**
     * 查询履约保函列表
     */
    @GetMapping("/historyList")
    public TableDataInfo historyList(BAgreement bAgreement)
    {
        startPage();
        List<BAgreement> list = bAgreementService.selectHistoryBAgreementList(bAgreement);
        return getDataTable(list);
    }

    /**
     * 一键复制
     */
    @GetMapping("/copySave/{id}")
    public AjaxResult copySave(@PathVariable Long id)
    {
        BAgreement bAgreement = bAgreementService.selectBAgreementById(id);
        String qureSql = "";
        BAgreementPosition position = new BAgreementPosition();
        position.setbSfxz(1L);
        List<BAgreementPosition> positionList = ibAgreementPositionService.selectBAgreementPositionList(position);
        for (BAgreementPosition bAgreementPosition:positionList){
            if (qureSql==""){
                qureSql=bAgreementPosition.getbValue()+" as "+bAgreementPosition.getbName();
            }else {
                qureSql=qureSql+","+bAgreementPosition.getbValue()+" as "+bAgreementPosition.getbName();
            }

        }
        if (qureSql==""){
            return AjaxResult.success();
        }
        bAgreement.setQureSql(qureSql);
        Map<String,String> maps = bAgreementService.selectMapList(bAgreement);

        return AjaxResult.success(maps);
    }

}
